﻿namespace ImageProcessing.IMG_Processor
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public static class IMG_GrayScale
    {
        /// <summary>
        /// converts the image into grey scale 
        /// </summary>
        /// <param name="img_OldBuffer">old image buffer</param>
        /// <returns>new buffer with grey values</returns>
        public static Entities.IMG_Image IMG_GrayScaleBuffer(Entities.IMG_Image img_OldBuffer)
        {
            try
            {
                Entities.Pixel img_NewPixel;
                Entities.IMG_Image img_NewBuffer = new Entities.IMG_Image(img_OldBuffer.Width, img_OldBuffer.Height);
                int index = img_OldBuffer.Width * img_OldBuffer.Height;
                for (int i = 0; i < index; i++)
                {
                    img_NewPixel = new Entities.Pixel(
                        ((img_OldBuffer.ImagePixels[i].Red + img_OldBuffer.ImagePixels[i].Green + img_OldBuffer.ImagePixels[i].Blue) / 3),
                        ((img_OldBuffer.ImagePixels[i].Red + img_OldBuffer.ImagePixels[i].Green + img_OldBuffer.ImagePixels[i].Blue) / 3),
                        ((img_OldBuffer.ImagePixels[i].Red + img_OldBuffer.ImagePixels[i].Green + img_OldBuffer.ImagePixels[i].Blue) / 3));
                    img_NewBuffer.ImagePixels[i] = img_NewPixel;
                }

                return img_NewBuffer;
            }
            catch (Exception img_ex)
            {
                Logger.LogException(img_ex);
                throw;
            }
        }
    }
}
